<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>矩阵统计(matrix_stats) | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-matrix-stats-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-matrix-stats-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-matrix-stats-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-matrix-stats-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-matrix-stats-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-matrix.html">矩阵聚合</a></span>
»
<span class="breadcrumb-node">矩阵统计(matrix_stats)</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-matrix.html">« 矩阵(matrix)聚合</a>
</span>
<span class="next">
<a href="caching-heavy-aggregations.html">重度缓存的聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-matrix-stats-aggregation"></a>矩阵统计(matrix_stats)
</h2>
</div></div></div>
<p>
<code class="literal">matrix_stats</code>聚合是一种数值聚合，它对一组文档字段计算以下统计信息：
</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<code class="literal">count</code>
</p>
</td>
<td valign="top">
<p>
计算中包含的每字段样本数。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">mean</code>
</p>
</td>
<td valign="top">
<p>
每个字段的平均值。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">variance</code>
</p>
</td>
<td valign="top">
<p>
(方差,离散)每个字段的测量对样本平均值的分布情况。
<span class="remark">(原文: Per field Measurement for how spread out the samples are from the mean.</span>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">skewness</code>
</p>
</td>
<td valign="top">
<p>
(偏度)每个字段的测量量化平均值周围的不对称分布。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">kurtosis</code>
</p>
</td>
<td valign="top">
<p>
(峰态)对分布形状进行量化的字段测量。
<span class="remark">(原文: Per field measurement quantifying the shape of the distribution.</span>
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">covariance</code>
</p>
</td>
<td valign="top">
<p>
(协方差)定量描述一个领域中的变化如何与另一个领域相关联的矩阵。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">correlation</code>
</p>
</td>
<td valign="top">
<p>
协方差(covariance)矩阵限定在-1到1（包括-1和1）范围内。

描述字段分布之间的关系。
</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>下面的例子演示了如何使用矩阵统计来描述收入和贫困之间的关系。</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "aggs": {
        "statistics": {
            "matrix_stats": {
                "fields": ["poverty", "income"]
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/587.console"></div>
<p>
聚合类型是<code class="literal">matrix_stats</code>，<code class="literal">fields</code>设置定义了用于计算统计数据的一组字段(数组格式)。

上述请求返回以下响应：
</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "statistics": {
            "doc_count": 50,
            "fields": [{
                "name": "income",
                "count": 50,
                "mean": 51985.1,
                "variance": 7.383377037755103E7,
                "skewness": 0.5595114003506483,
                "kurtosis": 2.5692365287787124,
                "covariance": {
                    "income": 7.383377037755103E7,
                    "poverty": -21093.65836734694
                },
                "correlation": {
                    "income": 1.0,
                    "poverty": -0.8352655256272504
                }
            }, {
                "name": "poverty",
                "count": 50,
                "mean": 12.732000000000001,
                "variance": 8.637730612244896,
                "skewness": 0.4516049811903419,
                "kurtosis": 2.8615929677997767,
                "covariance": {
                    "income": -21093.65836734694,
                    "poverty": 8.637730612244896
                },
                "correlation": {
                    "income": -0.8352655256272504,
                    "poverty": 1.0
                }
            }]
        }
    }
}</pre>
</div>
<p><code class="literal">doc_count</code>字段指示统计计算中涉及的文档数量。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_multi_value_fields"></a>多值字段
</h3>
</div></div></div>
<p>
<code class="literal">matrix_stats</code>聚合将每个文档字段视为一个独立的样本。

参数<code class="literal">mode</code>控制聚合将对数组或多值字段使用什么数组值。

该参数的值可以是下列之一：
</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<code class="literal">avg</code>
</p>
</td>
<td valign="top">
<p>
(默认) 使用所有值的平均值。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">min</code>
</p>
</td>
<td valign="top">
<p>
选择最低值。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">max</code>
</p>
</td>
<td valign="top">
<p>
选择最高值。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">sum</code>
</p>
</td>
<td valign="top">
<p>
使用所有值的总和。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">median</code>
</p>
</td>
<td valign="top">
<p>
使用所有值的中位数。
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_missing_values_3"></a>缺失的值
</h3>
</div></div></div>
<p>
参数<code class="literal">missing</code>定义应该如何处理有缺失值的文档。 默认情况下，它们将被忽略，但也可以将它们视为有一个值。

这是通过添加一组 fieldname : value 映射来指定每个字段的默认值来实现的。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_search
{
    "aggs": {
        "matrixstats": {
            "matrix_stats": {
                "fields": ["poverty", "income"],
                "missing": {"income" : 50000} <a id="CO278-1"></a><i class="conum" data-value="1"></i>
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/588.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO278-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
字段<code class="literal">income</code>中没有值的文档将使用默认值<code class="literal">50000</code>。
</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_script_16"></a>脚本
</h3>
</div></div></div>
<p>此聚合系列尚不支持脚本。</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-matrix.html">« 矩阵(matrix)聚合</a>
</span>
<span class="next">
<a href="caching-heavy-aggregations.html">重度缓存的聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>